package com.cskaoyan.mall.cart.controller;

import com.cskaoyan.mall.cart.api.dto.CartInfoDTO;
import com.cskaoyan.mall.cart.service.CartService;
import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.common.util.AuthContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/7/15 10:15
 */
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 1. 添加商品到购物车
     * 浏览器 → 网关(全局过滤器) → 购物车服务
     * @param skuId
     * @param skuNum
     * @param request
     */
    @RequestMapping("cart/add/{skuId}/{skuNum}")
    public Result addCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum, HttpServletRequest request) {

        //2. 获取userId，要是userId为空，就获取临时用户Id
        //String userId = request.getHeader("userId");
        String userId = AuthContext.getUserId(request);  //获取登录的userId
        if (StringUtils.isBlank(userId)){
            userId = AuthContext.getUserTempId(request); //获取未登录的userTempId
        }

        //1. 添加商品到购物车
        cartService.addToCart(skuId, userId, skuNum);
        return Result.ok();
    }

    /**
     * 2. 获取用户购物车列表
     * @param request
     */
    @GetMapping("/cart")
    public Result cartList(HttpServletRequest request) {

        //1. 获取用户id（已登录/未登录）
        String userId = AuthContext.getUserId(request);
        String userTempId = AuthContext.getUserTempId(request);

        // 调用购物车服务，获取用户的购物车列表
        // 如果userId不为空且userTempId也不为空，那么意味着要合并购物车，并删除用户临时用户的购物车
        List<CartInfoDTO> cartList = cartService.getCartList(userId, userTempId);

        return Result.ok(cartList);
    }

    /**
     * 3. 更新选中状态
     * @param skuId
     * @param isChecked
     * @param request
     * @return
     */
    @PutMapping("cart/check/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked, HttpServletRequest request){

        //1. 获取用户id（已登录/未登录）
        String userId = AuthContext.getUserId(request);
        if (StringUtils.isBlank(userId)){
            userId = AuthContext.getUserTempId(request);
        }

        //2. 修改选中状态
        cartService.checkCart(userId, isChecked, skuId);

        return Result.ok();
    }

    /**
     * 4. 删除购物车对应的商品
     * @param skuId
     * @param request
     * @return
     */
    @DeleteMapping("/cart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId, HttpServletRequest request) {

        //1. 获取用户id（已登录/未登录）
        String userId = AuthContext.getUserId(request);
        if (StringUtils.isBlank(userId)){
            userId = AuthContext.getUserTempId(request);
        }

        //2. 删除购物车对应的商品
        cartService.deleteCart(skuId, userId);

        return Result.ok();
    }

    /**
     * 4. 删除购物车中已选中的商品
     * @param request
     * @return
     */
    @DeleteMapping("/cart/checked")
    public Result deleteChecked(HttpServletRequest request) {

        //1. 获取用户id（已登录/未登录）
        String userId = AuthContext.getUserId(request);
        if (StringUtils.isBlank(userId)){
            userId = AuthContext.getUserTempId(request);
        }

        //2. 删除购物车中已选中的商品
        cartService.deleteChecked(userId);

        return Result.ok();
    }
}
